package org.jfk.sys.dictionary;

import org.apache.log4j.Logger;
import org.jfk.core.controller.BaseController;
import org.jfk.sys.user.User;

public class DictController extends BaseController {
	private static Logger logger = Logger.getLogger(DictController.class);  

	public void add(){
		
	}

	public void edit() {
		Dict dict = Dict.dao.findById(getParaToInt());
		if (dict == null){
			dict = new Dict();
		}
		
		try{
			int dict_type = this.getParaToInt("dict_type");
			dict.set("dict_type", dict_type);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		setAttr("dict", dict);
	}

	public void save() throws Exception {
		
		Dict dict = getModel(Dict.class);
		if (dict.get("id") == null){
			User currUser = super.getCurrUser();
			dict.set("creator", currUser.getInt("id"));			
			dict.save();
		}else{
			//对于可以人工输入ID的场景需要增加此判断
			Object o = Dict.dao.findById(dict.get("id"));
			if (o == null){
				dict.save();
			}else{
				dict.update();
			}
		}
		super.renderJson();
	}
	
	
	
	public void list() {
		listColumns = new String[][] { 
				{ "id", "编号" },
				{ "dict_type_cnName", "类型" }, 
				{ "dict_value", "值" },
				{ "description", "描述" }, 
				{ "creator_loginName", "创建人" }
				};

		super.setAttrs();
		
		setAttr("page",Dict.dao.paginate(this));
		
	}
	
	public void del() {
		String id = getPara("id");
		Dict.dao.deleteById(id);
		super.renderText("test del");
	}

	public void delAll() {
		Integer[] checkedId = super.getParaValuesToInt("id[]");

		for (int i = 0; checkedId != null && i < checkedId.length; i++) {
			Dict.dao.deleteById(checkedId[i]);
		}
		super.renderText("test delAll");
	}
	
}
